set more off

cap cd "C:\Users\ejm5\Dropbox\PAPI conjoint\Replication"
cap cd "/Users/paulschuler/Dropbox/projects/PAPI conjoint\Replication"

/*Create Form A*/
use "RawData\statapapi963_final_national.dta", replace
mvdecode idcode0 tinh tentinh huyen tenhuyen tenxa tenthon khuvuc a00* a016* a011* d305* d101d*  d101b* d102* d407    PSW* villageid  d306*, mv(111=.a\888=.b\999=.c)
save "PAPI2018.dta", replace

#delimit;
destring villageid, replace;
format villageid %20.0f;
sort villageid;


rename id respondent_id;

keep idcode0 tinh tentinh huyen tenhuyen tenxa tenthon khuvuc a00* a016* a011* d305* d101d* d101b* d102* d407    PSW* villageid respondent_id d306*;
mvdecode d102* , mv(-999999=.);

generate year=2018;
generate form=1;

#delimit;

/*Age*/
generate age_39=1 if d102_age1==1;
replace age_39=0 if d102_age1==2;

generate age_59=1 if d102_age1==2;
replace age_59=0 if d102_age1==1;

/*Sex*/
generate male=1 if  d102_sex1==1;
replace male=0 if  d102_sex1==2;

generate female=1 if  d102_sex1==2;
replace female=0 if  d102_sex1==1;

/*Home Province*/
rename d102_prov1 home_province;

/*Education*/
generate high_school=1 if  d102_edu1==1;
replace high_school=0 if  d102_edu1!=1;

generate  bachelors=1 if  d102_edu1==2;
replace  bachelors=0 if  d102_edu1!=2;

generate  masters=1 if  d102_edu1==3;
replace  masters=0 if  d102_edu1!=3;

generate  doctorate=1 if  d102_edu1==4;
replace  doctorate=0 if  d102_edu1!=4;


/*Nomination Status*/
generate self_nominee=1 if  d102_nom1==1;
replace self_nominee=0 if  d102_nom1==2;

generate gov_nominee=1 if  d102_nom1==2;
replace gov_nominee=0 if  d102_nom1==1;


/*Party Status*/
generate party=1 if  d102_party1==1;
replace party=0 if  d102_party1==2;

generate non_party=1 if  d102_party1==2;
replace non_party=0 if  d102_party1==1;


/*Job*/
generate VFF=.;
replace VFF=1 if  d102_job1==1;
replace VFF=0 if  d102_job1!=1 & d102_job1!=.;

generate business=.;
replace business=1 if  d102_job1==2;
replace  business=0 if  d102_job1!=2 & d102_job1!=.;

generate doctor=.;
replace  doctor=1 if  d102_job1==3;
replace  doctor=0 if  d102_job1!=3 & d102_job1!=.;

generate lawyer=.;
replace  lawyer=1 if  d102_job1==4;
replace  lawyer=0 if  d102_job1!=4 & d102_job1!=.;


/*Policy*/
generate trade=1 if  d102_plan1==1;
replace trade=0 if  d102_plan1!=1;

generate poverty=1 if  d102_plan1==2;
replace  poverty=0 if  d102_plan1!=2;

generate  growth=1 if  d102_plan1==3;
replace  growth=0 if  d102_plan1!=3;

generate  environment=1 if  d102_plan1==4;
replace  environment=0 if  d102_plan1!=4;

generate  no_policy=1 if  d102_plan1==5;
replace  no_policy=0 if  d102_plan1!=5;


/*Experience*/
generate exp_zero=.;
replace exp_zero=1 if  d102_exp1==1;
replace exp_zero=0 if  d102_exp1!=1 & d102_exp1!=.;

generate exp_y5=.;
replace exp_y5=1 if  d102_exp1==2;
replace  exp_y5=0 if  d102_exp1!=2  & d102_exp1!=.;

generate exp_y15=.;
replace  exp_y15=1 if  d102_exp1==3;
replace  exp_y15=0 if  d102_exp1!=3  & d102_exp1!=.;


/*Vote VNA*/
generate vote=1 if d102a==1;
replace vote=0 if d102a==2;
replace vote=1 if d102b==1;
replace vote=0 if d102b==2;

generate vote_job=1 if d102a==1;
replace vote_job=0 if d102a==2;
generate vote_exp=1 if d102b==1;
replace vote_exp=0 if d102b==2;


/*Vote Village*/
generate vote_village=1 if d102c==1;
replace vote_village=0 if d102c==2;
replace vote_village=1 if d102d==1;
replace vote_village=0 if d102d==2;

save NA_formA.dta, replace;


/*Create Form B*/
#delimit;
set more off;
use  "PAPI2018.dta", clear;



rename id respondent_id;


keep idcode0 tinh tentinh huyen tenhuyen tenxa tenthon khuvuc a00* a016* a011* d305* d101d* d101b* d102* d407   PSW* villageid respondent_id;
mvdecode d102* , mv(-999999=.);

generate year=2018;
generate form=2;

#delimit;

/*Age*/
generate age_39=1 if d102_age2==1;
replace age_39=0 if d102_age2==2;

generate age_59=1 if d102_age2==2;
replace age_59=0 if d102_age2==1;

/*Sex*/
generate male=1 if  d102_sex2==1;
replace male=0 if  d102_sex2==2;

generate female=1 if  d102_sex2==2;
replace female=0 if  d102_sex2==1;

/*Home Province*/
rename d102_prov1 home_province;

/*Education*/
generate high_school=1 if  d102_edu2==1;
replace high_school=0 if  d102_edu2!=1;

generate  bachelors=1 if  d102_edu2==2;
replace  bachelors=0 if  d102_edu2!=2;

generate  masters=1 if  d102_edu2==3;
replace  masters=0 if  d102_edu2!=3;

generate  doctorate=1 if  d102_edu2==4;
replace  doctorate=0 if  d102_edu2!=4;


/*Nomination Status*/
generate self_nominee=1 if  d102_nom2==1;
replace self_nominee=0 if  d102_nom2==2;

generate gov_nominee=1 if  d102_nom2==2;
replace gov_nominee=0 if  d102_nom2==1;


/*Party Status*/
generate party=1 if  d102_party2==1;
replace party=0 if  d102_party2==2;

generate non_party=1 if  d102_party2==2;
replace non_party=0 if  d102_party2==1;


/*Job*/
generate VFF=.;
replace VFF=1 if  d102_job2==1;
replace VFF=0 if  d102_job2!=1 & d102_job2!=.;

generate business=.;
replace business=1 if  d102_job2==2;
replace  business=0 if  d102_job2!=2 & d102_job2!=.;

generate doctor=.;
replace  doctor=1 if  d102_job2==3;
replace  doctor=0 if  d102_job2!=3 & d102_job2!=.;

generate lawyer=.;
replace  lawyer=1 if  d102_job2==4;
replace  lawyer=0 if  d102_job2!=4 & d102_job2!=.;



/*Policy*/
generate trade=1 if  d102_plan2==1;
replace trade=0 if  d102_plan2!=1;

generate poverty=1 if  d102_plan2==2;
replace  poverty=0 if  d102_plan2!=2;

generate  growth=1 if  d102_plan2==3;
replace  growth=0 if  d102_plan2!=3;

generate  environment=1 if  d102_plan2==4;
replace  environment=0 if  d102_plan2!=4;

generate  no_policy=1 if  d102_plan2==5;
replace  no_policy=0 if  d102_plan2!=5;


/*Experience*/
generate exp_zero=.;
replace exp_zero=1 if  d102_exp2==1;
replace exp_zero=0 if  d102_exp2!=1 & d102_exp2!=.;

generate exp_y5=.;
replace exp_y5=1 if  d102_exp2==2;
replace  exp_y5=0 if  d102_exp2!=2  & d102_exp2!=.;

generate exp_y15=.;
replace  exp_y15=1 if  d102_exp2==3;
replace  exp_y15=0 if  d102_exp2!=3  & d102_exp2!=.;

/*Vote VNA*/
generate vote=1 if d102a==2;
replace vote=0 if d102a==1;
replace vote=1 if d102b==2;
replace vote=0 if d102b==1;

generate vote_job=1 if d102a==2;
replace vote_job=0 if d102a==1;
generate vote_exp=1 if d102b==2;
replace vote_exp=0 if d102b==1;

/*Vote Village*/
generate vote_village=1 if d102c==2;
replace vote_village=0 if d102c==1;
replace vote_village=1 if d102d==2;
replace vote_village=0 if d102d==1;



save NA_formB.dta, replace;

/*Append Together*/
#delimit;
use NA_formA.dta, clear;
append using NA_formB.dta;

#delimit;
xtset respondent_id form;
generate policy_same=1 if trade==1 & l.trade==1;
replace policy_same=1 if trade==1 & f.trade==1;
replace policy_same=1 if poverty==1 & l.poverty==1;
replace policy_same=1 if poverty==1 & f.poverty==1;
replace policy_same=1 if growth==1 & l.growth==1;
replace policy_same=1 if growth==1 & f.growth==1;
replace policy_same=1 if environment==1 & l.environment==1;
replace policy_same=1 if environment==1 & f.environment==1;
replace policy_same=1 if no_policy==1 & l.no_policy==1;
replace policy_same=1 if no_policy==1 & f.no_policy==1;
replace policy_same=0 if policy_same==.;

generate experience_same=1 if exp_zero==1 & l.exp_zero==1;
replace experience_same=1 if exp_zero==1 & f.exp_zero==1;
replace experience_same=1 if exp_y5==1 & l.exp_y5==1;
replace experience_same=1 if exp_y5==1 & f.exp_y5==1;
replace experience_same=1 if exp_y15==1 & l.exp_y15==1;
replace experience_same=1 if exp_y15==1 & f.exp_y15==1;
replace experience_same=0 if experience_same!=1;

generate quality_same=1 if high_school==1 & l.high_school==1;
replace quality_same=1 if high_school==1 & f.high_school==1;
replace quality_same=1 if bachelor==1 & l.bachelor==1;
replace quality_same=1 if bachelor==1 & f.bachelor==1;
replace quality_same=1 if master==1 & l.master==1;
replace quality_same=1 if master==1 & f.master==1;
replace quality_same=1 if doctorate==1 & l.doctorate==1;
replace quality_same=1 if doctorate==1 & f.doctorate==1;
replace quality_same=0 if experience_same!=1;

gen no_false = .;
replace no_false=1 if d101d5a==1;
replace no_false=1 if d101d5b==1;
replace no_false=0 if d101d5a==0;
replace no_false=0 if d101d5b==0;

#delimit;
gen home=1 if home_province==tinh;
replace home=0 if home_province !=tinh;
lab var policy_same "Candidates Share Policy Priority";
save PAPI_Conjoint_2018.dta, replace;

/*Append 2016 and 2017 Data Together*/
use PAPI_Conjoint_2018.dta, replace;
append using  PAPI_Conjoint_2years.dta, force;




#delimit cr
mvdecode age* , mv(-999999=.)
mvdecode sex* , mv(-999999=.)
mvdecode home_province* , mv(-999999=.)
mvdecode education* , mv(-999999=.)
mvdecode party_status* , mv(-999999=.)
mvdecode job* , mv(-999999=.)
mvdecode policy_priority* , mv(-999999=.)
mvdecode nomination* , mv(-999999=.)
save PAPI_Conjoint_3years.dta, replace











